#!/bin/bash
# mongodb数据库自动化安装文件
# Author:  linzening@qq.com

if [ ! -f "MongoDB-init-CentOS" ];then
    wget https://hello.fe80.cn/bash/lib/MongoDB-init-CentOS
fi

oneinstack_dir=$(dirname "`readlink -f $0`")
mongo_data_dir=/data/mongodb
if [ "$(getconf WORD_BIT)" == "32" ] && [ "$(getconf LONG_BIT)" == "64" ]; then
    OS_BIT=64
    SYS_BIT_j=x64 #jdk
    SYS_BIT_a=x86_64 #mariadb
    SYS_BIT_b=x86_64 #mariadb
    SYS_BIT_c=x86_64 #ZendGuardLoader
    SYS_BIT_d=x86-64 #ioncube
else
    OS_BIT=32
    SYS_BIT_j=i586
    SYS_BIT_a=x86
    SYS_BIT_b=i686
    SYS_BIT_c=i386
    SYS_BIT_d=x86
fi

mongodb_ver=4.2.12
sys_ver=rhel80
mongo_install_dir=/usr/local/mongodb
dbmongopwd="alphaxxxx"
echo=echo
CSI=$($echo -e "\033[")
CEND="${CSI}0m"
CFAILURE="$CRED"
OS="CentOS"

Install_MongoDB(){
    pushd . > /dev/null
    # pushd ${oneinstack_dir}/src > /dev/null
    echo "[info]  正在配置mongod用户"
    id -u mongod >/dev/null 2>&1
    [ $? -ne 0 ] && useradd -s /sbin/nologin mongod
    echo "[info]  正在解压软件包"
    tar xzf ./mongodb-linux-${SYS_BIT_b}-${sys_ver}-${mongodb_ver}.tgz
    if [ -d "${mongo_data_dir}" ];then
        echo "[ERROR] ${mongo_data_dir}目录已存在，请先删除"
        exit 1
    fi
    mkdir -p ${mongo_data_dir};chown mongod.mongod -R ${mongo_data_dir}
    /bin/mv ./mongodb-linux-${SYS_BIT_b}-${sys_ver}-${mongodb_ver} ${mongo_install_dir}
    [ "${OS}" == "CentOS" ] && { cp ./MongoDB-init-CentOS /etc/init.d/mongod; sed -i "s@/usr/local/mongodb@${mongo_install_dir}@g" /etc/init.d/mongod; chkconfig --add mongod; chkconfig mongod on; }
    [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]] && { /bin/cp ../init.d/MongoDB-init-Ubuntu /etc/init.d/mongod; sed -i "s@/usr/local/mongodb@${mongo_install_dir}@g" /etc/init.d/mongod; update-rc.d mongod defaults; }
    chmod 755 /etc/init.d/mongod
    echo "[info]  正在配置配置文件"
    cat > /etc/mongod.conf << EOF
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
    destination: file
    logAppend: true
    path: ${mongo_data_dir}/mongod.log

# Where and how to store data.
storage:
    dbPath: ${mongo_data_dir}
    journal:
        enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
    fork: true  # fork and run in background
    pidFilePath: /var/run/mongodb/mongod.pid

# network interfaces
net:
    port: 27017
    bindIp: 0.0.0.0
    unixDomainSocket:
        enabled: false

#security:
#  authorization: enabled

#operationProfiling:
#replication:
#sharding:
EOF
    service mongod start
    echo ${mongo_install_dir}/bin/mongo 127.0.0.1/admin --eval \"db.createUser\(\{user:\'root\',pwd:\'$dbmongopwd\',roles:[\'userAdminAnyDatabase\']\}\)\" | bash
    sed -i 's@^#security:@security:@' /etc/mongod.conf
    sed -i 's@^#  authorization:@  authorization:@' /etc/mongod.conf
    if [ -e "${mongo_install_dir}/bin/mongo" ]; then
        # sed -i "s+^dbmongopwd.*+dbmongopwd='$dbmongopwd'+" ../options.conf
        echo "${CSUCCESS}MongoDB installed successfully! ${CEND}"
        rm -rf ./mongodb-linux-${SYS_BIT_b}-${mongodb_ver}
        echo "MongoDB Restart..."
        service mongod restart
    else
        rm -rf ${mongo_install_dir} ${mongo_data_dir}
        echo "${CFAILURE}MongoDB install failed, Please contact the author! ${CEND}"
        kill -9 $$
    fi
    popd
    [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${mongo_install_dir}/bin:\$PATH" >> /etc/profile
    [ -n "$(grep ^'export PATH=' /etc/profile)" -a -z "$(grep ${mongo_install_dir} /etc/profile)" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${mongo_install_dir}/bin:\1@" /etc/profile
    . /etc/profile
    # 软链
    ln -sf /usr/local/mongodb/bin/mongo /usr/local/bin/
}

Do_Download()
{
    if [ ! -f "./mongodb-linux-${SYS_BIT_b}-${sys_ver}-${mongodb_ver}.tgz" ];then
        # mongodb-linux-x86_64-4.0.0.tgz
        # https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.0.tgz
        echo "[info]  正在下载mongo安装文件"
        wget -4 https://fastdl.mongodb.org/linux/mongodb-linux-${SYS_BIT_b}-${sys_ver}-${mongodb_ver}.tgz -P .
        if [ ! -f "./mongodb-linux-${SYS_BIT_b}-${sys_ver}-${mongodb_ver}.tgz" ];then
            echo "[error]  下载失败"
        else
            echo "[info]  下载成功，继续安装"
            Install_MongoDB
        fi
    else
        echo "[info]  安装包已存在，继续安装"
        Install_MongoDB
    fi
}

Do_Download

if [ `grep -c "mongodb" /usr/local/php/etc/php.ini` -eq '0' ]; then
    pecl install mongodb
    # 如果mongodb安装失败，请执行以下代码手动安装，需要开启exec函数
    # curl 'http://pecl.php.net/get/mongodb-1.9.0.tgz' -o mongodb-1.9.0.tgz && pecl install mongodb-1.9.0.tgz
    echo "安装完成"
    service php-fpm restart
fi



# mongo admin -u root -p alphaxxxx
# 运行完成后手动执行这一句：
#              use admin;
#              db.grantRolesToUser ( "root", [ { role: "__system", db: "admin"}])

# 备份代码
# mongodump -h 127.0.0.1 -d linzening -u "root" -p "axxxx9" --authenticationDatabase "admin" -o /data/0604

# mongorestore -h 127.0.0.1:27017 --db linzening -u root -p axxxx9 --authenticationDatabase "admin" .